
package DAO;

import Utils.HibernateUtil;
import controller.SendEmail;
import java.util.List;
import javax.faces.context.FacesContext;
import model.User;
import org.apache.commons.mail.EmailException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import validator.e;

public class UserDA extends BaseDAO<User>{


   public void save(User user) throws EmailException {
     Session session = HibernateUtil.getSessionFactory().openSession(); 
     Transaction t = session.beginTransaction();
     SendEmail se = new SendEmail();
    if(consult(user.getEmail())){
        try{
             session.save(user);
             t.commit();
             se.sendMail(user);
             e.exibirMsgSucesso("Sucess");
         }catch(HibernateException err){
             e.exibirMsgErro("Error - Try Again");
         }
    }else{
            e.exibirMsgErro("Error - Email already registered");
         }
    }
    
        public List<User> consultUser(String email) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query= session.createSQLQuery("select * from User where email = :email and status = 0").addEntity(User.class);
        query.setParameter("email", email);
        List lista = query.list();
        session.close();
        return lista;
    }

    @Override
    public List<User> list() {  
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    Query query= session.createSQLQuery("select * from User").addEntity(User.class);
    List lista = query.list();
    session.close();
    return lista;
    }

    @Override
    public void update(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        try{
            session.update(user);
            t.commit();
            e.exibirMsgSucesso("Sucess");
        }
        catch(HibernateException err){
            e.exibirMsgErro("Error");
        }
        session.close();
    }
    
    @Override
    public void remove(User user) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.delete(user);
        t.commit();
        session.close();
    }

    @Override
    public boolean consult(String email) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query= session.createSQLQuery("select * from User where email = :email").addEntity(User.class);
        query.setParameter("email",email);
        List lista = query.list();
        if(lista == null || lista.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
    
    public User getUser(String email,String password){
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    try{
        Query query = session.createSQLQuery("Select * From User where email = :email and password = :password and status = 0").addEntity(User.class);
        query.setParameter("email",email);
        query.setParameter("password",password);
        List<User> u = query.list();
        return u.get(0);
    }catch(Exception e)
    {
        return null;
    }
}
    
}
